package com.qj.yantaishan.controller;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qj.yantaishan.common.annotation.Log;
import com.qj.yantaishan.common.enums.BusinessType;
import com.qj.yantaishan.domain.UserAddress;
import com.qj.yantaishan.service.IUserAddressService;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.AjaxResult;
import com.qj.yantaishan.common.utils.poi.ExcelUtil;
import com.qj.yantaishan.common.core.page.TableDataInfo;

/**
 * 收货地址Controller
 * 
 * @author qihe
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/yantaishan/UserAddress")
public class UserAddressController extends BaseController
{
    private String prefix = "yantaishan/UserAddress";

    @Autowired
    private IUserAddressService userAddressService;

    @RequiresPermissions("yantaishan:address:view")
    @GetMapping()
    public String address()
    {
        return prefix + "/address";
    }

    /**
     * 查询收货地址列表
     */
    @RequiresPermissions("yantaishan:address:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserAddress userAddress)
    {
        startPage();
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        return getDataTable(list);
    }

    /**
     * 导出收货地址列表
     */
    @RequiresPermissions("yantaishan:address:export")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserAddress userAddress)
    {
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        return util.exportExcel(list, "收货地址数据");
    }

    /**
     * 新增收货地址
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存收货地址
     */
    @RequiresPermissions("yantaishan:address:add")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserAddress userAddress)
    {
        return toAjax(userAddressService.insertUserAddress(userAddress));
    }

    /**
     * 修改收货地址
     */
    @RequiresPermissions("yantaishan:address:edit")
    @GetMapping("/edit/{addressId}")
    public String edit(@PathVariable("addressId") Integer addressId, ModelMap mmap)
    {
        UserAddress userAddress = userAddressService.selectUserAddressByAddressId(addressId);
        mmap.put("userAddress", userAddress);
        return prefix + "/edit";
    }

    /**
     * 修改保存收货地址
     */
    @RequiresPermissions("yantaishan:address:edit")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserAddress userAddress)
    {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除收货地址
     */
    @RequiresPermissions("yantaishan:address:remove")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userAddressService.deleteUserAddressByAddressIds(ids));
    }
}
